/****************************************************************
 * <strong>MFOperation</strong>
 *
 * <p>
 * MFOperation
 * </p>
 *
 * @author AndyZhang 2011
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.common;

import com.thinkwide.data.core.MFString;

import javax.servlet.ServletRequest;

public class MFOperation {

    public static final String datatype = "datatype";
    public static final String sql = "sql";
    public final static String ajax = "ajax";
    public final static String sort = "sort";
    public final static String field = "field";
    public final static String rowtype = "rowtype";

    public final static String tableName = "_table";
    /**
     * 支持逗号
     */
    public final static String tableDetailName = "_table_detail";
    public final static String value = "_value";

    public final static String start = "_start";
    public final static String limit = "_limit";
    public final static String id = "_id";

    public static final String operation = "_operation";
    public static final String modify = "_modify";
    public static final String delete = "_delete";
    public static final String insert = "_insert";
    public static final String select = "_select";

    public static final String blank = "_blank";
    public static final String none = "_none";
    public static final String mfstate = "_mfstate";

    public static final String key = "_key";
    public static final String begin = "_begin";
    public static final String end = "_end";
    public static final String pageindex = "_pageindex";

    public static Boolean isInsert(String state) {
        return MFString.compareNoCase(insert, state);
    }

    /**
     * @param state
     * @return
     */
    public static Boolean isModify(String state) {
        return MFString.compareNoCase(modify, state);
    }

    public static Boolean isSysKey(String value) {
        if (MFString.isNullOrEmpty(value))
            return false;
        else
            return value.startsWith("_");
    }

    public static Boolean isStateKey(String value) {
        return MFString.compareNoCase(mfstate, value);
    }

    public static Boolean isDelete(String state) {
        return MFString.compareNoCase(delete, state);
    }

    public static Boolean isSelect(String state) {
        return MFString.compareNoCase(select, state);
    }

    public static MFOperationType getOperationType(ServletRequest request) {
        String oper = request.getParameter(operation);
        if (MFString.notNullOrEmpty(oper)) {
            if (MFString.compareNoCase(modify, oper)) return MFOperationType.modify;
            if (MFString.compareNoCase(delete, oper)) return MFOperationType.delete;
            if (MFString.compareNoCase(insert, oper)) return MFOperationType.insert;
            if (MFString.compareNoCase(blank, oper)) return MFOperationType.blank;
            if (MFString.compareNoCase(select, oper)) return MFOperationType.select;
            //if (MFString.compareNoCase(edit, oper)) return MFOperationType.edit;
        }

        return MFOperationType.none;
    }
}
